<template>
  <!-- 登录模块 -->
  <div class="login">
    <el-form class="box" :model="form" :rules="rules" ref="form">
      <div class="log">
        <img src="@/assets/common/login-logo.png" alt="" />
      </div>
      <el-form-item prop="mobile">
        <el-input v-model="form.mobile"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button class="btn" @click="submit">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// 导入登录请求接口
// import { login } from '@/api/user.js'
export default {
  data () {
    return {
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      // 表单验证
      rules: {
        mobile: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('请输入正确格式手机号'))
                this.$message.success('登录成功')
              }
            },
            trigger: 'change'
          }
        ],
        password: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 6, max: 12, message: '请输入6-12位密码' }
        ]
      }
    }
  },
  methods: {
    submit () {
      this.$refs.form.validate(async result => {
        if (result) {
          await this.$store.dispatch('user/toLogin', this.form)
          // const res = login(this.form)
          // console.log('res:', res)
          this.$message.success('登录成功')
          // 跳转到首页
          // 获取回跳地址参数, redirect无值就跳转首页
          const redirect = this.$route.query.redirect || '/'
          this.$router.push(redirect)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background: url(~@/assets/common/login.jpg);
  display: flex;
  justify-content: center;
  align-items: center;
  .box {
    width: 520px;
    height: 400px;
    padding: 35px 35px 18px 35px;
    .log {
      text-align: center;
      padding-bottom: 40px;
    }
    .btn {
      width: 100%;
      padding: 20px;
      background: #407ffe;
      border: none;
      color: #fff;
      font-size: 24px;
    }
    ::v-deep .el-input__inner {
      height: 50px;
      background: #d4e5ff;
    }
    ::v-deep .el-form-item.is-error .el-input__inner,
    .el-form-item.is-error .el-input__inner:focus,
    .el-form-item.is-error .el-textarea__inner,
    .el-form-item.is-error .el-textarea__inner:focus,
    .el-message-box__input input.invalid,
    .el-message-box__input input.invalid:focus {
      border: none;
    }
  }
}
</style>
